<template>
  <div>
    <home-header :city="city"></home-header>
    <HomeSwiper :list="swiperList"></HomeSwiper>
    <HomeIcons :list="iconList"></HomeIcons>
    <HomeLabel :list="recommendList"></HomeLabel>
    <Homesunday :list="weekendList"></Homesunday>
  </div>
</template>

<script>
import HomeHeader from "./components/HomeHeader";
import HomeSwiper from "./components/Swiper";
import HomeIcons from "./components/Icons";
import Homesunday from "./components/sunday";
import HomeLabel from "./components/label";
import axios from "axios";
import { mapState } from "vuex";
export default {
  name: "Home",
  components: {
    HomeHeader,
    HomeSwiper,
    HomeIcons,
    HomeLabel,
    Homesunday
  },
  data() {
    return {
      swiperList: [],
      iconList: [],
      recommendList: [],
      weekendList: [],
      lastcity: ""
    };
  },
  methods: {
    getHomeInfo() {
      axios
        .get("/static/mork/index.json?city=" + this.city)
        .then(this.getHomeInfoSucc);
    },
    getHomeInfoSucc(res) {
      res = res.data;
      if (res.ret && res.data) {
        const data = res.data;
        this.swiperList = data.swiperList;
        this.iconList = data.iconList;
        this.recommendList = data.recommendList;
        this.weekendList = data.weekendList;
      }
    }
  },
  beforeRouteLeave(to, from, next) {
    try {
      if(to.name=='detaill'){
      let top = document.documentElement.scrollTop || document.body.scrollTop; // 得到当前高
      localStorage.top = top;
      }
    }catch (err) {}
    next(); //一定不要忘记写
  },
  beforeRouteEnter(to, from, next) {
    if (to.name == "Home") {
      setTimeout(() => {
        scroll(0,localStorage.top);
      }, 10);
    }
    next();
  },
  computed: {
    ...mapState(["city"])
  },
  activated() {
    if (this.lastcity != this.city) {
      this.lastcity = this.city;
      this.getHomeInfo();
    }
  },
  mounted() {
    this.lastcity = this.city;
    this.getHomeInfo();
  }
};
</script>

<style lang="stylus" scoped></style>
